package lk.mrt.cse.wihidum.storage;

import lk.mrt.cse.wihidum.storage.*;
import java.util.*;
import org.apache.log4j.*;
/**
 * Created by IntelliJ IDEA.
 * User: Wihidum
 * Date: Mar 26, 2010
 * Time: 11:24:35 AM
 * To change this template use File | Settings | File Templates.
 */
public class BrokerSubscriptionMap
{
    private static final Logger logger=Logger.getLogger(BrokerSubscriptionMap.class);
    private static Map<String ,BrokerSubscription> brokerSubscriptionMap=new HashMap<String,BrokerSubscription>();
    public static void saveSubscriber(String uuid,BrokerSubscription brokerSubscription)
    {
        brokerSubscriptionMap.put(uuid,brokerSubscription);
    }
    public static ArrayList<BrokerSubscription> getBrokerSubscribersFilteredByTopic(String topic)
    {
        ArrayList<BrokerSubscription> filteredBrokerSubscribers=new ArrayList<BrokerSubscription>();
        String[] topicSplitArray = topic.split("#");
        ArrayList<String> filters = new ArrayList<String>();
        String temp = topicSplitArray[0];

        for(int i = 0; i < topicSplitArray.length; i++)
        {
            if(i == 0)
                filters.add(temp);
            else
            {
                temp = temp.concat("#"+topicSplitArray[i]);
                filters.add(temp);
            }
        }

       for(String filter: filters)
       {
            for(Map.Entry<String,BrokerSubscription> entry:brokerSubscriptionMap.entrySet())
            {
                String filterTopic=entry.getValue().getTopic();
                String fromBrokerAddress=entry.getValue().getFromBrokerAddress();

                if( fromBrokerAddress != null && !fromBrokerAddress.isEmpty() && filter.equals(filterTopic))
                {
                    filteredBrokerSubscribers.add(entry.getValue());
                }
            }
       }   

        return filteredBrokerSubscribers;
    }
    public static boolean isSubscribedTo(NeighbourBroker neighbourBroker,String topic)
    {
        boolean isSubscribed=false;
        for(Map.Entry<String,BrokerSubscription> entry: brokerSubscriptionMap.entrySet())
        {
               String brokerID=entry.getValue().getToBrokerID();
               String filterTopic=entry.getValue().getTopic();
            if(brokerID != null && brokerID.equals(neighbourBroker.getBrokerID()) && filterTopic.equals(topic))
            {
                 isSubscribed=true;
                 break;
            }

        }
        return isSubscribed;

    }

    public static void clearBrokerSubscriptionMap()
    {
        brokerSubscriptionMap.clear();
    }
    public static String getBrokerSubscriberOnTopic(String topic)
       {
          String toAddress=null;
          for(Map.Entry<String,BrokerSubscription> entry:brokerSubscriptionMap.entrySet())
          {
               String entryTopic=entry.getValue().getTopic();
               if(topic.equals(entryTopic))
               {
                   String toBrokerAddress=entry.getValue().getToBrokerAddress();
                   logger.info("BrokerSubscription Fond on the topic:"+topic+"at"+toBrokerAddress);
                   if(toBrokerAddress!=null)
                   {
                      toAddress=toBrokerAddress;
                      break;
                   }

               }
          }
          return toAddress;

      }
    public static void deleteToBrokerSubscriber(String toBrokerAddress)
       {
          for(Map.Entry<String,BrokerSubscription> entry:brokerSubscriptionMap.entrySet())
          {
               String address=entry.getValue().getToBrokerAddress();

               if(address!=null && toBrokerAddress.equals(address))
               {
                   brokerSubscriptionMap.remove(entry.getKey());
                   break;
               }
          }

      }
    public static void deleteFromBrokerSubscriber(String fromBrokerAddress)
       {
          for(Map.Entry<String,BrokerSubscription> entry:brokerSubscriptionMap.entrySet())
          {
               String address=entry.getValue().getFromBrokerAddress();

               if(address!=null && fromBrokerAddress.equals(address))
               {
                   brokerSubscriptionMap.remove(entry.getKey());
                   break;
               }
          }

      }
}
